var $passwordModal;

// 设置菜单激活状态
var renderMenu = function() {
	var pathname = document.location.pathname;

	$('ul.nav.nav-sidebar li a').click(function(event) {
		var $this = $(this);
		var token = querystring('token');
		document.location.assign($this.attr('href') + '?token=' + token);
		event.preventDefault();
	});

	$('ul.nav.nav-sidebar li a[href="' + pathname + '"]').parent().addClass('active');
};﻿

// 设置用户名称
var renderUsername = function() {
	$('#username').text(Cookies.get('_username'));
};



$(function() {
	// 密码修改窗口
	$passwordModal = $('#password_modal').on('hidden.bs.modal', function (e) {
		$('#password_modal input:password').val('');
		$passwordModal.find('.has-error').removeClass('has-error');
		$passwordModal.find('.alert').hide();
	});

	$('#password_modal .password_save_btn').click(function() {
		putCustomerPassword({
			oldPassword: $.trim($('#password_modal input.oldPassword').val()),
			password: $.trim($('#password_modal input.password').val())
		}, function(err, result) {
			if (err) {
				var $errMsg = $passwordModal.find('.alert').hide().empty();
				if (result) {
					if (result.errors) {
						$.each(result.errors, function(key, errObj) {
							var $formGroup = $passwordModal.find('.' + key).parents('.form-group').addClass('has-error');
							$formGroup.find('.alert').append(errObj.message).show();
						});
					}
				}
				return;
			}
			alert('修改成功。');
			$passwordModal.modal('hide');
		});
	});

	renderMenu();
	renderUsername();
});
